﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace oqis.core.common.winsys
{

    /// <summary>
    /// 
    /// </summary>
    public class NativeMethods
    {
        [DllImport("kernel32")]
        public static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
        [DllImport("kernel32")]
        public static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);


        /// <summary>
        /// 获取系统监视器
        /// </summary>
        /// <param name="hwnd"></param>
        /// <param name="dwFlags"></param>
        /// <returns></returns>
        [DllImport("user32.dll", EntryPoint = "MonitorFromWindow", SetLastError = true)]
        public static extern IntPtr MonitorFromWindow(
            [In] IntPtr hwnd, uint dwFlags);

        /// <summary>
        /// 获取显示器数量
        /// </summary>
        /// <param name="hMonitor"></param>
        /// <param name="pdwNumberOfPhysicalMonitors"></param>
        /// <returns></returns>
        [DllImport("dxva2.dll", EntryPoint = "GetNumberOfPhysicalMonitorsFromHMONITOR", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool GetNumberOfPhysicalMonitorsFromHMONITOR(
            IntPtr hMonitor, ref uint pdwNumberOfPhysicalMonitors);

        /// <summary>
        /// 获取物理显示器
        /// </summary>
        /// <param name="hMonitor"></param>
        /// <param name="dwPhysicalMonitorArraySize"></param>
        /// <param name="pPhysicalMonitorArray"></param>
        /// <returns></returns>
        [DllImport("dxva2.dll", EntryPoint = "GetPhysicalMonitorsFromHMONITOR", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool GetPhysicalMonitorsFromHMONITOR(
            IntPtr hMonitor,
            uint dwPhysicalMonitorArraySize,
            [Out] NativeStructures.PHYSICAL_MONITOR[] pPhysicalMonitorArray);

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dwPhysicalMonitorArraySize"></param>
        /// <param name="pPhysicalMonitorArray"></param>
        /// <returns></returns>
        [DllImport("dxva2.dll", EntryPoint = "DestroyPhysicalMonitors", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool DestroyPhysicalMonitors(
            uint dwPhysicalMonitorArraySize, [Out] NativeStructures.PHYSICAL_MONITOR[] pPhysicalMonitorArray);

        /// <summary>
        /// 获取显示器类型
        /// </summary>
        /// <param name="hMonitor"></param>
        /// <param name="pdtyDisplayTechnologyType"></param>
        /// <returns></returns>
        [DllImport("dxva2.dll", EntryPoint = "GetMonitorTechnologyType", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool GetMonitorTechnologyType(
            IntPtr hMonitor, ref NativeStructures.MC_DISPLAY_TECHNOLOGY_TYPE pdtyDisplayTechnologyType);

        /// <summary>
        /// 获取显卡？
        /// </summary>
        /// <param name="hMonitor"></param>
        /// <param name="pdwMonitorCapabilities"></param>
        /// <param name="pdwSupportedColorTemperatures"></param>
        /// <returns></returns>
        [DllImport("dxva2.dll", EntryPoint = "GetMonitorCapabilities", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool GetMonitorCapabilities(
            IntPtr hMonitor, ref uint pdwMonitorCapabilities, ref uint pdwSupportedColorTemperatures);

        /// <summary>
        /// 查找窗体
        /// </summary>
        /// <param name="lpClassName"></param>
        /// <param name="lpWindowName"></param>
        /// <returns></returns>
        [DllImport("User32.dll ", EntryPoint = "FindWindow")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        /// <summary>
        /// 查找窗体
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="childe"></param>
        /// <param name="strclass"></param>
        /// <param name="strname"></param>
        /// <returns></returns>
        [DllImport("User32.dll ")]
        public static extern IntPtr FindWindowEx(IntPtr parent, IntPtr childe, string strclass, string strname);


    }

    /// <summary>
    /// 常量
    /// </summary>
    public class NativeConstants
    {
        /// <summary>
        /// 
        /// </summary>
        public const int MONITOR_DEFAULTTOPRIMARY = 1;

        /// <summary>
        /// 
        /// </summary>
        public const int MONITOR_DEFAULTTONEAREST = 2;

        /// <summary>
        /// 
        /// </summary>
        public const int MONITOR_DEFAULTTONULL = 0;
    }

    /// <summary>
    /// 结构体
    /// </summary>
    public class NativeStructures
    {
        /// <summary>
        /// 
        /// </summary>
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
        public struct PHYSICAL_MONITOR
        {
            /// <summary>
            /// 
            /// </summary>
            public IntPtr hPhysicalMonitor;

            /// <summary>
            /// 
            /// </summary>
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
            public string szPhysicalMonitorDescription;
        }


        /// <summary>
        /// 类型变量
        /// </summary>
        public enum MC_DISPLAY_TECHNOLOGY_TYPE
        {
            /// <summary>
            /// 
            /// </summary>
            MC_SHADOW_MASK_CATHODE_RAY_TUBE,

            /// <summary>
            /// 
            /// </summary>
            MC_APERTURE_GRILL_CATHODE_RAY_TUBE,

            /// <summary>
            /// 
            /// </summary>
            MC_THIN_FILM_TRANSISTOR,

            /// <summary>
            /// 
            /// </summary>
            MC_LIQUID_CRYSTAL_ON_SILICON,

            /// <summary>
            /// 
            /// </summary>
            MC_PLASMA,

            /// <summary>
            /// 
            /// </summary>
            MC_ORGANIC_LIGHT_EMITTING_DIODE,

            /// <summary>
            /// 
            /// </summary>
            MC_ELECTROLUMINESCENT,

            /// <summary>
            /// 
            /// </summary>
            MC_MICROELECTROMECHANICAL,
            /// <summary>
            /// 
            /// </summary>
            MC_FIELD_EMISSION_DEVICE,
        }
    }

}
